#!/bin/bash

user_name=layty
sanmba_name=work
samba_file=/home/$user_name/"$sanmba_name"
tftp_file=/home/$user_name/tftpboot
nfs_file="$samba_file"

RED_COLOR='\E[1;31m'
PINK_COLOR='\E[1;35m'
YELOW_COLOR='\E[1;33m'
BLUE_COLOR='\E[1;34m'
GREEN_COLOR='\E[1;32m'
END_COLOR='\E[0m'
PLAIN='\033[0m'

check_root() {
    if [ $(id -u) != "0" ]; then
        echo -e "${RED_COLOR}Error: This script must be run as root!${END_COLOR}"
        exit 1
    fi
}

check_user_name() {
    cat /etc/passwd|grep $user_name
    if [ $? -eq 0 ];then
        # echo -e "${BLUE_COLOR}Check the set user name OK.${END_COLOR}"
                echo "check_user_name ok"
    else
        #sudo   useradd -m $user_name   -G root -p lyt
                echo "check_user_name error : please add user $user_name "
                exit 1
    fi
}

check_network() {
    ping -c 1 www.baidu.com > /dev/null 2>&1
    if [ $? -eq 0 ];then
        echo -e "${BLUE_COLOR}Network OK.${END_COLOR}"
    else
        echo -e "${RED_COLOR}Network failure!${END_COLOR}"
        exit 1
    fi
}

check_status() {
    ret=$?
    if [ "$ret" -ne "0" ]; then
        echo -e "${RED_COLOR}Failed setup, aborting..${END_COLOR}"
        exit 1
    fi
}

samba_configure() {
    echo "--------samba_configure----"
    mkdir -p $samba_file
    local back_file=/etc/samba/smb.conf.bakup
    if [ ! -e "$back_file" ];then
        cp /etc/samba/smb.conf $back_file
    fi
    check_status

    grep "$samba_file" /etc/samba/smb.conf 1>/dev/null
    if [ $? -ne 0 ];then
        sed -i \
        '$a['$sanmba_name']\
        path = '$samba_file'\
        available = yes\
        public = yes\
        valid users = '$user_name'\
        guest ok = yes\
        read only = no\
        writeable = yes\n' /etc/samba/smb.conf

        #/etc/init.d/samba restart
        service smbd restart
        passwd="$user_name"123 
        (echo $passwd;echo $passwd) | smbpasswd -a $user_name
        service smbd restart
    fi
}

tftp_configure() {
    if [ ! -d "$tftp_file" ];then
        mkdir -p -m 777 $tftp_file
    fi

    grep $tftp_file /etc/default/tftpd-hpa 1>/dev/null
    if [ $? -ne 0 ];then
        sed  -i '$a\TFTP_DIRECTORY="'$tftp_file'"' /etc/default/tftpd-hpa
        sed  -i '$a\TFTP_OPTIONS="-l -c -s"' /etc/default/tftpd-hpa
    fi
    service tftpd-hpa restart
}

nfs_configure() {
    if [ ! -d "$nfs_file" ];then
        mkdir -p -m 777 $nfs_file
    fi

    grep $nfs_file /etc/exports 1>/dev/null
    if [ $? -ne 0 ];then
        sed -i '$a\'$nfs_file'/  *(rw,nohide,insecure,no_subtree_check,async,no_root_squash)' /etc/exports  #todo
    fi
    service nfs-kernel-server restart
}

vscode(){
    mkdir -p /tmp/vscode_down
    cd /tmp/vscode_down
    wget https://az764295.vo.msecnd.net/stable/e713fe9b05fc24facbec8f34fb1017133858842b/code_1.56.1-1620296372_amd64.deb
    dpkg -i ./*
    code --install-extension C/C++
    code --install-extension ms-vscode.cpptools
    code --install-extension twxs.cmake
    code --install-extension ms-vscode.cmake-tools
    code --install-extension cheshirekow.cmake-format
    code --install-extension fredericbonnet.cmake-test-adapter
}

codeserver()
{
    curl -fsSL https://code-server.dev/install.sh | sh
}



zlggcc()
{
    mkdir -p /opt/zlg/
    cd /opt/zlg/
    wget http://192.168.11.165:3000/layty/public/raw/master/linux/gcc-linaro-arm-linux-gnueabihf-4.9-2014.09_linux.tar.bz2
    tar jxvf gcc-linaro-arm-linux-gnueabihf-4.9-2014.09_linux.tar.bz2
    #rm gcc-linaro-arm-linux-gnueabihf-4.9-2014.09_linux.tar.bz2
    
    grep /opt/zlg/gcc-linaro-arm-linux-gnueabihf-4.9-2014.09_linux /etc/profile 1>/dev/null
    if [ $? -ne 0 ];then
        sed -i.bak '$a export ZLGCC=/opt/zlg/gcc-linaro-arm-linux-gnueabihf-4.9-2014.09_linux \nexport PATH=$ZLGCC/bin:$PATH' /etc/profile
        echo "add zlg gcc in /etc/profile "
    else
        echo " zlg gcc exists "
    fi
}

vim_configure() {
    git clone --depth=1 https://github.com/amix/vimrc.git /home/$user_name/.vim_runtime

    touch /home/$user_name/.vim_runtime/my_configs.vim
    echo "let g:go_version_warning = 0" > /home/$user_name/.vim_runtime/my_configs.vim

    chown -R $user_name /home/$user_name/.vim_runtime
    chmod u+x /home/$user_name/.vim_runtime/install_awesome_vimrc.sh
    su - $user_name -s /home/$user_name/.vim_runtime/install_awesome_vimrc.sh

        grep "alias vi=vim" /etc/profile 1>/dev/null
        if [ $? -ne 0 ];then
                sed  -i '$a\alias vi=vim' /etc/profile
                echo "add alias vi=vim in /etc/profile "
        else
                echo "alias vi=vim exists "
        fi
}


# git config --global http.proxy 'socks5://192.168.30.106:7890'
# git config --global https.proxy 'socks5://192.168.30.106:7890'
# export http_proxy="192.168.30.111:7890"
# export https_proxy="192.168.30.111:7890"
# export ftp_proxy='192.168.30.111:7890'
#samba_configure
#tftp_configure
#nfs_configure
#vscode
#zlggcc
#vim_configur